package com.bw.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.order.service.TbHouseOrderService;
import com.bw.order.mapper.TbHouseOrderMapper;
import com.bw.po.TbDictionary;
import com.bw.po.TbHouseOrder;
import com.bw.result.ResponseState;
import com.bw.result.ResultVo;
import com.bw.util.SnowflakeIdWorker;
import com.bw.vo.IdVo;
import com.bw.vo.TbHouseOrderSearchVo;
import com.bw.vo.TbHouseOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author lenovo
* @description 针对表【tb_house_order(房源订单)】的数据库操作Service实现
* @createDate 2023-11-11 16:26:37
*/
@Service
public class TbHouseOrderServiceImpl extends ServiceImpl<TbHouseOrderMapper, TbHouseOrder>
    implements TbHouseOrderService{

    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0,0);

    @Autowired
    private TbHouseOrderMapper tbHouseOrderMapper;

    @Override
    public ResultVo del(IdVo idVo) {

        boolean flag = removeById(idVo.getId());

        return flag?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.DELETE_ERROR);
    }

    @Override
    public ResultVo add(TbHouseOrderVo tbHouseOrderVo) {
        tbHouseOrderVo.setId(String.valueOf(idWorker.nextId()));
        //生成时间戳
        long number = System.currentTimeMillis();
        //订单号
        tbHouseOrderVo.setOrderNum(String.valueOf(number));

        TbHouseOrder tbHouseOrder = new TbHouseOrder();
        BeanUtils.copyProperties(tbHouseOrderVo,tbHouseOrder);
        int flag = tbHouseOrderMapper.insert(tbHouseOrder);
        return flag>0?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.INSERT_ERROR);
    }

    @Override
    public ResultVo updateByVo(TbHouseOrderVo tbHouseOrderVo) {
        TbHouseOrder tbHouseOrder = new TbHouseOrder();
        BeanUtils.copyProperties(tbHouseOrderVo,tbHouseOrder);
        int flag = tbHouseOrderMapper.updateById(tbHouseOrder);
        return flag>0?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.UPDATE_ERROR);
    }

    @Override
    public ResultVo getList(TbHouseOrderSearchVo tbHouseOrderSearchVo) {
        Page<TbHouseOrder> page = new Page<>(tbHouseOrderSearchVo.getPageNum(), tbHouseOrderSearchVo.getPageSize());
        Page<TbHouseOrderVo> pageList = tbHouseOrderMapper.getList(page,tbHouseOrderSearchVo);
        return ResultVo.SUCCESS(pageList);
    }

    @Override
    public ResultVo getByVo(TbHouseOrderSearchVo tbHouseOrderSearchVo) {
        TbHouseOrderVo tbHouseOrderVo = tbHouseOrderMapper.getByVo(tbHouseOrderSearchVo);
        return ResultVo.SUCCESS(tbHouseOrderVo);
    }


}




